package system.service.sys;

import framework.base.BaseService;
import framework.base.BatchService;
import framework.base.ListQueryService;
import system.dto.sys.SysStateQuery;
import system.dto.sys.SysStateResult;
import system.entity.SysState;
import system.mapper.SysStateMapper;

import java.util.Date;

public interface SysStateService extends BaseService<SysState, Long>, ListQueryService<SysStateResult, SysStateQuery>, BatchService<SysState, SysStateMapper> {
    default Class<SysStateMapper> getMapperClass() {
        return SysStateMapper.class;
    }

    /**
     * Updates the body of a SysState entity.
     * 更新SysState实体的主体内容。
     *
     * @param param SysState 实体
     * @return 返回更新结果，0表示未更新，1表示更新成功。
     */
    int updateBody(SysState param);

    /**
     * Returns the default group name for states.
     * 返回默认的状态组名。
     *
     * @return
     */
    default String defaultGroupName() {
        return "default";
    }

    /**
     * Returns the default node name for states.
     * 返回默认的状态节点名。
     *
     * @return
     */
    default String defaultNodeName() {
        return "-";
    }

    /**
     * Returns the state value for the given name in the default group.
     * 返回默认组中指定名称的状态值。
     *
     * @param name 状态名称
     * @return 状态值
     */
    default SysState loadState(String name) {
        return loadState(defaultGroupName(), name, defaultNodeName());
    }

    /**
     * Returns the state value for the given group name and name in the default node.
     * 返回指定组名和名称在默认节点中的状态值。
     *
     * @param groupName 状态组名
     * @param name      状态名称
     * @return 状态值
     */
    default SysState loadState(String groupName, String name) {
        return loadState(defaultGroupName(), name, defaultNodeName());
    }

    /**
     * Returns the state value for the given group name and state name.
     * 返回指定组名和状态名称的状态值。
     *
     * @param groupName 状态组名
     * @param name      状态名称
     * @param nodeName  状态节点名
     * @return 状态值
     */
    SysState loadState(String groupName, String name, String nodeName);

    default Long loadInteger(String groupName, String name, String nodeName) {
        SysState state = loadState(groupName, name, nodeName);
        if (state != null && state.getVi() != null)
            return state.getVi();
        return null;
    }

    default Double loadDouble(String groupName, String name, String nodeName) {
        SysState state = loadState(groupName, name, nodeName);
        if (state != null && state.getVd() != null)
            return state.getVd();
        return null;
    }

    default String loadString(String groupName, String name, String nodeName) {
        SysState state = loadState(groupName, name, nodeName);
        if (state != null && state.getVs() != null)
            return state.getVs();
        return null;
    }

    default String loadString(String name) {
        return loadString(defaultGroupName(), name, defaultNodeName());
    }

    default String loadString(String groupName, String name) {
        return loadString(groupName, name, defaultNodeName());
    }

    default Long loadInteger(String name) {
        return loadInteger(defaultGroupName(), name, defaultNodeName());
    }

    default Long loadInteger(String groupName, String name) {
        return loadInteger(groupName, name, defaultNodeName());
    }

    default Double loadDouble(String name) {
        return loadDouble(defaultGroupName(), name, defaultNodeName());
    }

    default Double loadDouble(String groupName, String name) {
        return loadDouble(groupName, name, defaultNodeName());
    }

    default int saveState(String groupName, String name, String nodeName, Long vi, Date expireTime) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setNodeName(nodeName);
        state.setVi(vi);
        state.setExpireTime(expireTime);
        return add(state);
    }

    default int saveState(String groupName, String name, String nodeName, Double vd, Date expireTime) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setNodeName(nodeName);
        state.setVd(vd);
        state.setExpireTime(expireTime);
        return add(state);
    }

    default int saveState(String groupName, String name, String nodeName, String vs, Date expireTime) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setNodeName(nodeName);
        state.setVs(vs);
        state.setExpireTime(expireTime);
        return add(state);
    }


    default int saveState(String groupName, String name, Long vi) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setVi(vi);
        return add(state);
    }

    default int saveState(String groupName, String name, Double vd) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setVd(vd);
        return add(state);
    }

    default int saveState(String groupName, String name, String vs) {
        SysState state = new SysState();
        state.setGroupName(groupName);
        state.setName(name);
        state.setVs(vs);
        return add(state);
    }


    default int saveState(String name, Long vi, Date expireTime) {
        SysState state = new SysState();
        state.setName(name);
        state.setVi(vi);
        state.setExpireTime(expireTime);
        return add(state);
    }

    default int saveState(String name, Double vd, Date expireTime) {
        SysState state = new SysState();
        state.setName(name);
        state.setVd(vd);
        state.setExpireTime(expireTime);
        return add(state);
    }

    default int saveState(String name, String vs, Date expireTime) {
        SysState state = new SysState();
        state.setName(name);
        state.setVs(vs);
        state.setExpireTime(expireTime);
        return add(state);
    }

    default int saveState(String name, Long vi) {
        SysState state = new SysState();
        state.setName(name);
        state.setVi(vi);
        return add(state);
    }

    default int saveState(String name, Double vd) {
        SysState state = new SysState();
        state.setName(name);
        state.setVd(vd);
        return add(state);
    }

    default int saveState(String name, String vs) {
        SysState state = new SysState();
        state.setName(name);
        state.setVs(vs);
        return add(state);
    }

}